package cn.fsd.controller;

import cn.fsd.domain.Commodity;

import cn.fsd.query.delivery.CommodityClassQuery;
import cn.fsd.query.delivery.CommodityQuery;
import cn.fsd.service.impl.CommodityServiceImpl;
import cn.fsd.vo.CommodityNameClassBean;
import cn.fsd.vo.ResultBean;

import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/commodity")
public class CommodityController {

  @Autowired
  CommodityServiceImpl commodityService;

  @PostMapping
  @ApiOperation(value = "新增商品，一个商品码可能对应多个商品记录")
  public ResultBean addCommodity(@RequestBody Commodity commodity) {
    if (commodity == null) {
      return ResultBean.error("请求参数不能为空");
    }
    Commodity entry = commodityService.findCommodityByName(commodity.getCommodity_name());
    if (entry != null) {
      return ResultBean.error("商品名称重复");
    }

    return ResultBean.success((Object)commodityService.addCommodity(commodity));
  }

  @DeleteMapping("/{id}")
  @ApiOperation(value = "删除商品")
  public ResultBean deleteCommodity(@PathVariable(value = "id") String commodityId) {
    if (commodityId == null || StringUtils.isEmpty(commodityId)) {
      return ResultBean.error("请求参数不能为空");
    }
    commodityService.delCommodity(commodityId);
    return ResultBean.success();
  }

  @GetMapping("/all")
  @ApiOperation(value = "查询全部商品")
  public ResultBean findAllCommodity() {

    return ResultBean.success(commodityService.findAllCommodity());

  }

  @GetMapping("/{id}")
  @ApiOperation(value = "根据id查询商品")
  public ResultBean findCommodityById(@PathVariable(value = "id") String id) {
    return ResultBean.success(commodityService.findCommodityById(id));
  }

  @PostMapping("/page")
  @ApiOperation(value = "分页查询商品")
  public ResultBean findCommodityByPage(@RequestBody CommodityQuery query) {
    return ResultBean.success(commodityService.findByPage(query));
  }

  @PutMapping
  @ApiOperation(value = "更新商品")
  public ResultBean updateCommodity(@RequestBody Commodity commodity) {
    if (StringUtils.isEmpty(commodity.getCommodity_id())) {
      return ResultBean.error("商品ID不能为空");
    } else if (StringUtils.isEmpty(commodity.getCommodity_name())) {
      return ResultBean.error("商品名称不能为空");
    } else if (StringUtils.isEmpty(commodity.getFirst_class()) || StringUtils.isEmpty(
        commodity.getSecond_class())) {
      return ResultBean.error("商品分类不能为空");
    }
    commodityService.updateCommodity(commodity);
    return ResultBean.success();
  }

  @PostMapping("/getPatch")
  @ApiOperation(value = "根据id批量获取商品")
  public List<Commodity> getCommodityPatch(@RequestBody List<String> ids) {
    return commodityService.findCommodityPatch(ids);
  }

  @PostMapping("/getByClass")
  @ApiOperation(value = "根据商品分类获取商品")
  public ResultBean getCommodityByClass(@RequestBody CommodityClassQuery query) {
    if (!StringUtils.isEmpty(query.getSecondClass()) && StringUtils.isEmpty(
        query.getFirstClass())) {
      return ResultBean.error("二级分类的一级分类不能为空");
    }
    return ResultBean.success(commodityService.findCommodityByClass(query));
  }

  /**
   * 分站微服务需要调用的接口
   */
  @GetMapping("/getCommodityById")
  public Commodity getTheCommodity(@RequestParam("id") String id) {
    return commodityService.getTheCommodity(id);
  }

  @ApiOperation("根据分类id查询出商品列表")
  @PostMapping("/getCommodityByClass")
  public List<Commodity> getCommodities(@RequestBody CommodityNameClassBean bean) {
    return commodityService.getCommoditiesByClass(bean.getFirst_class(),bean.getSecond_class(), bean.getCommodityName());
  }
}
